package com.epro.dx.adapter;

import java.util.ArrayList;

import com.epro.dx.R;
import com.epro.dx.util.ImageLoaderUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.weipu.common.facade.model.ModuleChildrenModel;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/************************
 * @author Tony
 * @Date:2016-4-7下午3:08:03
 * @描述:消息列表数据适配器
 ************************/
public class PromotionListAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<ModuleChildrenModel> datas;
	private DisplayImageOptions imageLoaderOptions;
	private ImageLoader imageLoader;
	/**
	 * @param mContext 上下文
	 * @param isPromotion 是否是活动列表
	 * @param datas 数据模型列表
	 */
	public PromotionListAdapter(Context mContext,ArrayList<ModuleChildrenModel> datas){
		this.mContext = mContext;
		this.datas = datas;
		imageLoaderOptions = ImageLoaderUtils.getImageLoaderOptions(R.drawable.default1080_875, R.drawable.default1080_875);
		imageLoader = ImageLoader.getInstance();
	}
	@Override
	public int getCount() {
		//根据数据列表确定数量
		return null == datas?0:datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO 
		HolderView holder;
		if(null == convertView){
			holder = new HolderView();
			convertView = View.inflate(mContext,R.layout.item_promotion_list, null);
			holder.img = (ImageView) convertView.findViewById(R.id.item_message_event_list_img);
			holder.title = (TextView) convertView.findViewById(R.id.item_message_event_list_title);
			holder.smallTitle = (TextView) convertView.findViewById(R.id.item_message_event_list_smalltitle);
			holder.invalidActive = (TextView) convertView.findViewById(R.id.active_invalid);
			convertView.setTag(holder);
		}else{
			holder = (HolderView) convertView.getTag();
		}
		//数据处理
		ModuleChildrenModel model = datas.get(position);
		if(null!=model){
			//设置标题
			String name = model.getName();
			if(!TextUtils.isEmpty(name)){
				holder.title.setText(name);
			}
			//设置副标题
			String subName = model.getSubName();
			if(!TextUtils.isEmpty(subName)){
				holder.smallTitle.setVisibility(View.VISIBLE);
				holder.smallTitle.setText(subName);
			}else{
				holder.smallTitle.setVisibility(View.GONE);
			}
			//设置图片
			String imgUrl = model.getPhoneImg();
			if(!TextUtils.isEmpty(imgUrl)){
				holder.img.setVisibility(View.VISIBLE);
				imageLoader.displayImage(imgUrl, holder.img, imageLoaderOptions);
			}else{
				holder.img.setVisibility(View.GONE);
			}
			//判断活动是否有效
			if(model.getState()==3){
				holder.invalidActive.setVisibility(View.VISIBLE);
			}else{
				holder.invalidActive.setVisibility(View.GONE);
			}
		}
		return convertView;
	}

	class HolderView{
		ImageView img;
		TextView title;
		TextView smallTitle;
		TextView time;
		TextView invalidActive;
	}
}
